package com.software.frontend.fragments;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.google.android.material.tabs.TabLayout;
import com.software.frontend.R;
import com.software.frontend.firstPage.adapter.MyAdapter;
import com.software.frontend.firstPage.fragment.ConcernFragment;
import com.software.frontend.firstPage.fragment.RecFragment;
import com.software.frontend.searchmovie.activity.SearchMovieRequestActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link YouYouJIan_MainFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class YouYouJIan_MainFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "title";
    private static final String ARG_PARAM2 = "img";

    // TODO: Rename and change types of parameters
    private String title;
    private String img;
    //所需控件
    private ViewPager vp;
    private TabLayout tabLayout;
    private ImageButton imageButton;
    private List<Fragment> fragments;
    private List<String> titleList;
    private MyAdapter myAdapter;

    public YouYouJIan_MainFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param title Parameter 1.
     * @param img Parameter 2.
     * @return A new instance of fragment YouYouJIan_MainFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static YouYouJIan_MainFragment newInstance(String title, String img) {
        YouYouJIan_MainFragment fragment = new YouYouJIan_MainFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, title);
        args.putString(ARG_PARAM2, img);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            title = getArguments().getString(ARG_PARAM1);
            img = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_you_you_j_ian__main, container, false);


        /**
         * 在此处编写逻辑Fragment中的逻辑
         *
         * 页面跳转：
         * 需要在MainActivity中修改标注处，增加需要配置到Fragment池中的界面
         * 跳转源界面（本界面）跳转代码如下
         * this.getActivity().getSupportFragmentManager()
         *                 .beginTransaction()
         *                 .show()   //展示某界面
         *                 .hide()   //隐藏某界面
         *                 .commit();
         * */
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //获取控件
        vp = view.findViewById(R.id.vp);
        tabLayout = view.findViewById(R.id.tabLayout);
        initData();
        //注意此处，getChild 嵌套
        myAdapter = new MyAdapter(getChildFragmentManager(), fragments, titleList);
        vp.setAdapter(myAdapter);
        tabLayout.setupWithViewPager(vp);//tab 适配页面
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        imageButton = getActivity().findViewById(R.id.youYouJianTo);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchMovieRequestActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initData() {
        fragments = new ArrayList<>();
        RecFragment recFragment = RecFragment.newInstance("推荐");
        fragments.add(recFragment);
        fragments.add(new ConcernFragment());

        titleList = new ArrayList<>();
        titleList.add("推荐");
        titleList.add("关注");
    }
}